package com.java.payweb.controller;

import com.java.pay.entity.PaymentInfo;
import com.java.payweb.feign.PayFeign;
import com.java.payweb.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author jiangli
 * @since 2019/12/15 20:07
 */
@Controller
@RequestMapping("/pay")
public class PayController {
	@Autowired
	private PayFeign payFeign;
	@Autowired
	private PayService payService;

	/**
	 * http://jiangli.free.idcfengye.com/payweb-web/pay/payIndex?token=dhxys66lv59yg2sgix7xa5oh1ufjndn3
	 */
	@GetMapping("/payIndex")
	public void payIndex(@RequestParam("token") String token, HttpServletResponse resp) {
		resp.setContentType("text/html;charset=utf-8");
		PaymentInfo paymentInfo = payFeign.getPayInfoByToken(token);
		try (PrintWriter out = resp.getWriter()) {
			String html = payService.pay(paymentInfo);
			out.println(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
